/*
 * Project: Sudoku Explainer
 * Copyright (C) 2006-2007 Nicolas Juillerat
 * 
 * Project: Sudoku Hint Engine for Android
 * Copyright (C) 2010-2013 Emanuel Moecklin
 * 
 * Available under the terms of the Lesser General Public License (LGPL)
 */
package com.onegravity.sudoku.solver.rules.naked;

import java.util.BitSet;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;

import android.net.Uri;

import com.onegravity.sudoku.solver.HintResource;
import com.onegravity.sudoku.solver.HintType;
import com.onegravity.sudoku.solver.core.DirectHint;
import com.onegravity.sudoku.solver.core.Rule;
import com.onegravity.sudoku.solver.data.Cell;
import com.onegravity.sudoku.solver.data.Region;
import com.onegravity.sudoku.solver.util.HtmlLoader;

public class NakedSingleHint extends DirectHint implements Rule {

	private static final long serialVersionUID = 7488158989346575713L;

	private boolean mHasCandidates;
	
	public NakedSingleHint(Region region, Cell cell, int value, Collection<Cell> selectedCells, Map<Cell, BitSet> selectedPotentials, boolean hasCandidates) {
        super(region, cell, value, selectedCells, selectedPotentials);
        mHasCandidates = hasCandidates;
    }

    @Override
    public double getDifficulty() {
        return 2.3;
    }

    @Override
	public String getName() {
    	return mHasCandidates ? HintResource.NAKED_SINGLE.getName() : HintResource.NAKED_SINGLE_NO_CAND.getName();
    }

    @Override
    public Uri getLink() {
    	return mHasCandidates ? HintResource.NAKED_SINGLE.getUrl() : HintResource.NAKED_SINGLE_NO_CAND.getUrl();
	}

    @Override
    public Cell getCell(HintType type) {
        return (type==HintType.HINT) ? super.getCell(type) : null;
    }

    @Override
    public Collection<Cell> getSelectedCells(HintType hintType) {
    	if (mHasCandidates) {
        	return (hintType!=HintType.HINT) ? super.getSelectedCells(hintType) : null;
    	}
    	else {
        	return (hintType!=HintType.HINT) ? Collections.singletonList(super.getCell(hintType)) : null;
    	}
    }

    @Override
    public Map<Cell, BitSet> getSelectedPotentials(HintType hintType, int viewNum) {
    	return (mHasCandidates && hintType!=HintType.HINT) ? super.getSelectedPotentials(hintType, viewNum) : null;
    }

    @Override
    public String getHintHtml(HintType type) {
        String result = HtmlLoader.loadHtml(mHasCandidates ? HintResource.NAKED_SINGLE.hint(type) : HintResource.NAKED_SINGLE_NO_CAND.hint(type));
        return HtmlLoader.format(result, super.getCell(type).toString(), Integer.toString(super.getValue()));
    }

    @Override
    public String toString() {
        return getName() + ": " + super.toString(); //$NON-NLS-1$
    }

}